package cn.biplam.common.wrapper.tree;

import java.io.Serializable;
import java.util.ArrayList;

import cn.biplam.common.struc.BaseStruc.IndexTableStruc;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.sqlresult.SqlResult;

/**
 * 存储树的节点字段和索引表字段的映射关系
 * @author WangSong 2009-6-5下午03:48:20
 * @version 1.0
 */
public class TreeIndexFields implements Serializable{
	
	private static final long serialVersionUID = 2338245253653790917L;
	
	/**树的基本字段 */
	private String nodeId;
	private String nodeParent;
	private String nodeName;
	private String nodeDesc;
	private String isLeaf;
	private String isRoot;
	private String haveBrother;
	private String haveLBrother;
	private String nodeSequence;
	private String nodeLevel;
	private String operatorType;
	private String realId;
	private String nodeType;
	
	private TreeIndexFields(){}
	
	/**
	 * 索引树对应表名 
	 */
	private enum Tree_Index_Table{
		DEFAULT("DEFUALT"),
		MENU("MENU");
		private String value;
		private Tree_Index_Table(String value){this.value=value;}
		public String toString() {return value;}
	}
	
	/**
	 * 取节点id的映射字段
	 * @return nodeId
	 */
	public String getNodeId() {
		return nodeId;
	}
	
	/**
	 * 设置节点id的映射字段
	 * @param nodeId
	 */
	public void setNodeId(String nodeId) {
		this.nodeId = nodeId;
	}
	
	/**
	 * 取父节点的映射字段
	 * @return nodeParent
	 */
	public String getNodeParent() {
		return nodeParent;
	}
	
	/**
	 * 设置父节点的映射字段
	 * @param nodeParent
	 */
	public void setNodeParent(String nodeParent) {
		this.nodeParent = nodeParent;
	}
	
	/**
	 * 取节点名称映射字段
	 * @return nodeName
	 */
	public String getNodeName() {
		return nodeName;
	}
	
	/**
	 * 设置节点名称的映射字段
	 * @param nodeName
	 */
	public void setNodeName(String nodeName) {
		this.nodeName = nodeName;
	}
	
	/**
	 * 取节点描述的映射字段
	 * @return nodeDesc
	 */
	public String getNodeDesc() {
		return nodeDesc;
	}
	
	/**
	 * 设置节点描述的映射字段
	 * @param nodeDesc
	 */
	public void setNodeDesc(String nodeDesc) {
		this.nodeDesc = nodeDesc;
	}
	
	/**
	 * 取是否是叶子节点的映射字段
	 * @return isLeaf
	 */
	public String getIsLeaf() {
		return isLeaf;
	}
	
	/**
	 * 设置是否是是否是叶子节点的映射字段
	 * @param isLeaf
	 */
	public void setIsLeaf(String isLeaf) {
		this.isLeaf = isLeaf;
	}
	
	/**
	 * 取是否是根节点的映射字段
	 * @return isRoot
	 */
	public String getIsRoot() {
		return isRoot;
	}
	
	/**
	 * 设置是否是根节点的映射字段
	 * @param isRoot
	 */
	public void setIsRoot(String isRoot) {
		this.isRoot = isRoot;
	}
	
	/**
	 * 取是否有哥哥的映射字段
	 * @return haveBrother
	 */
	public String getHaveBrother() {
		return haveBrother;
	}
	
	/**
	 * 设置是否有哥哥的映射字段
	 * @param haveBrother
	 */
	public void setHaveBrother(String haveBrother) {
		this.haveBrother = haveBrother;
	}
	
	/**
	 * 取是否有弟弟的映射字段
	 * @return
	 */
	public String getHaveLBrother() {
		return haveLBrother;
	}
	
	/**
	 * 设置是否有弟弟的映射字段
	 * @param haveLBrother
	 */
	public void setHaveLBrother(String haveLBrother) {
		this.haveLBrother = haveLBrother;
	}
	
	/**
	 * 取节点排序的映射字段
	 * @return nodeSequence
	 */
	public String getNodeSequence() {
		return nodeSequence;
	}
	
	/**
	 * 设置点排序的映射字段
	 * @param nodeSequence
	 */
	public void setNodeSequence(String nodeSequence) {
		this.nodeSequence = nodeSequence;
	}
	
	/**
	 * 取节点级别的映射字段
	 * @return nodeLevel
	 */
	public String getNodeLevel() {
		return nodeLevel;
	}
	
	/**
	 * 设置节点级别的映射字段
	 * @param nodeLevel
	 */
	public void setNodeLevel(String nodeLevel) {
		this.nodeLevel = nodeLevel;
	}
	
	/**
	 * 取节点类型的映射字段
	 * @return
	 */
	public String getOperatorType() {
		return operatorType;
	}
	
	/**
	 * 设置节点类型的映射字段
	 * @param operatorType
	 */
	public void setOperatorType(String operatorType) {
		this.operatorType = operatorType;
	}
	
	/**
	 * 取树节点真实节点id对应的映射字段
	 * @return
	 */
	public String getRealId() {
		return realId;
	}
	
	/**
	 * 设置树节点真实节点id对应的映射字段
	 * @param realId
	 */
	public void setRealId(String realId) {
		this.realId = realId;
	}

	/**
	 * 取得索引表字段的个数
	 * @return
	 */
	public int getNodeLength(){
		int count = 13;
		return count;
	}

	/**
	 * 获取节点类型字段 
	 * @return 节点类型字段
	 */
	public String getNodeType() {
		return nodeType;
	}

	/**
	 * 设置节点类型字段
	 * @param nodeType 节点类型字段
	 */
	public void setNodeType(String nodeType) {
		this.nodeType = nodeType;
	}
	
	/**
	 * 获取指定表名索引表对应的树形字段映射关系
	 * @param tableName 表名
	 * @return 表名对应的树形字段映射关系
	 * @throws ErrorMessage 
	 */
	public static TreeIndexFields getTreeFields(String tableName) throws ErrorMessage{
		TOOLS.checkNull(tableName, "TreeIndexFields getTreeFields(String tableName) error,tableName");
		if(Tree_Index_Table.MENU.toString().equals(tableName.toUpperCase())){
			return getMenuTreeFields();
		}else{
			return getDefaultTreeFields();
		}
	}
	
	/**
	 * 将平面树形结果集数据放入树形链表<br>
	 * 用于获取绘制索引树的数据<br>
	 * @param sr      平面树形结果集 
	 * @param fields  结果集字段映射关系
	 * @param headId  头节点ID
	 * @param headLevel   头节点级别   
	 * @param haveBrother 是否有哥哥
	 * @param haveLBrother 是否有弟弟
	 * @param headParentsHaveLBrother 父亲们是否有弟弟
	 * @return 树形结果集数据对应的树形链表对象
	 * @throws ErrorMessage
	 */
	public static TreeStructre bindTreeDataToLinkList(SqlResult sr,TreeIndexFields fields,
			String headId,
			String headLevel,
			String haveBrother,
			String haveLBrother,
			String headParentsHaveLBrother) throws ErrorMessage{
		if(sr==null||sr.isNull()){
			return null;
		}
		TreeStructre tree=new TreeStructre();
		//创建头节点
		TreeNode node=createHead(headId, headLevel, haveBrother, haveLBrother, headParentsHaveLBrother);
		tree.insertHead(node);
		//创建其他节点
		for(int i=0;i<sr.size();i++){
			node=new TreeNode();
			node.setNodeID(sr.get(i, fields.getNodeId()));
			node.setParent(sr.get(i, fields.getNodeParent()));
			node.setNodeName(sr.get(i, fields.getNodeName()));
			node.setNodeCode(sr.get(i, fields.getRealId()));
			node.setNodeDesc(sr.get(i, fields.getNodeDesc()));
			node.setNodeType(sr.get(i, fields.getNodeType()));
			node.setOperator(sr.get(i, fields.getOperatorType()));
			tree.insert(node);
		}
		return tree;
	}
	
	/**
	 * 创建根节点
	 * @return 当前子树的根节点
	 */
	private static TreeNode createHead(String headId,
			String headLevel,
			String haveBrother,
			String haveLBrother,
			String headParentsHaveLBrother//客户端传入的父亲们是否有弟弟,可以为空,用"_"分割
			){
		//创建节点
		TreeNode node=null;
		node=new TreeNode(headId,"","");
		node.setHaveBrother(haveBrother);
		node.setHaveLBrother(haveLBrother);
		//设置节点级别
		if(headLevel==null){
			headLevel="1";
		}
		node.setLevel(headLevel);
		//设置节点是父亲们否有弟弟
		if(headParentsHaveLBrother!=null&&!"".equals(headParentsHaveLBrother)){
			String [] parentsLb=headParentsHaveLBrother.split("_");
			ArrayList<String> parentSibling=new ArrayList<String>();
			TOOLS.stringArrayToList(parentSibling, parentsLb);
			node.setParentSibling(parentSibling);
		}
		return node;
	}
	
	/**
	 * 业务模型对象索引表的字段映射关系
	 */
	private static TreeIndexFields getDefaultTreeFields(){
		TreeIndexFields fields=new TreeIndexFields();
		fields.setNodeId(IndexTableStruc.NODE_ID);
		fields.setNodeParent(IndexTableStruc.NODE_PARENT);
		fields.setNodeName(IndexTableStruc.NODE_NAME);
		fields.setNodeDesc(IndexTableStruc.NODE_DESC);
		fields.setNodeLevel(IndexTableStruc.NODE_LEVEL);
		fields.setNodeSequence(IndexTableStruc.NODE_SEQUENCE);
		fields.setOperatorType(IndexTableStruc.OPERATOR_TYPE);
		fields.setRealId(IndexTableStruc.NODE_CODE);
		fields.setNodeType(IndexTableStruc.NODE_TYPE);
		return fields;
	}
	
	/**
	 * 业务模型对象索引表的字段映射关系
	 */
	private static TreeIndexFields getMenuTreeFields(){
		TreeIndexFields fields=new TreeIndexFields();
		fields.setNodeId("MENU_ID");
		fields.setNodeParent("MENU_PARENT");
		fields.setNodeName("MENU_NAME");
		fields.setNodeDesc("MENU_DESC");
		fields.setNodeLevel("MENU_LEVEL");
		fields.setNodeSequence("MENU_SEQUENCE");
		fields.setOperatorType("PIC");
		fields.setRealId("ACTIONID");
		fields.setNodeType("node_type");
		return fields;
	}
}
